package com.dhl.deliver.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhl.deliver.entity.User;
import com.dhl.deliver.service.FilePathService;
import com.dhl.deliver.service.IUserService;
import com.dhl.deliver.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dhl
 * @since 2020-06-13
 */
@RestController
@RequestMapping("/user")
@Api("用户管理")
public class UserController {

    @Resource
    IUserService userService;

    @ApiOperation("获取当前系统登录用户")
    @GetMapping("/getCurrentUser")
    public Result getCurrentUser(HttpServletRequest request){    //普通获取数据
        String id = (String) request.getSession().getAttribute("id");
        if (id==null){
            return ResultUtil.error(ResultEnum.ERROR.getCode(),"用户尚未登录");
        }
        User user = userService.getById(id);
        Result result = ResultUtil.success(user);
        return result;
    }
    @GetMapping("/getUser")
    public Result getUser(){    //普通获取数据
        Result result = ResultUtil.success(userService.list());
        return result;
    }
    @ApiOperation("获取系统所有用户，分页。目前应该用不到")
    @GetMapping("/getUserByPage")
    public Result getUserByPage(int pageNum,int size){  //分页获取数据
        IPage<User> page=new Page<User>(pageNum,size);
        IPage<User> resultPage = userService.page(page);
        GridResultWrapper resultWrapper = new GridResultWrapper(resultPage.getTotal(),resultPage.getRecords() );
        Result result = ResultUtil.success(resultWrapper);
        return result;
    }
    @ApiOperation("用户登录，返回信息中告知是商家登录还是用户登录")
    @PostMapping("/login")
    public Result getUserByName(HttpServletRequest request,String id, String password){
        final User user = userService.getById(id);
        if (user!=null&&user.getPassword().equals(password))
            if (user.getRole().equals(Constant.ROLE_CUSTOMER)){
                request.getSession().setAttribute("id",id);
                return ResultUtil.success("顾客登录");
            }
            else if (user.getRole().equals(Constant.ROLE_SELLER)){
                request.getSession().setAttribute("id",id);
                return ResultUtil.success("商家登录");
        }
        return ResultUtil.result(ResultEnum.LOGINERROR);
    }

    @ApiOperation("顾客注册")
    @PostMapping("/register/customer")
    public Result registerCustomer(String id,String name,String password){
        final User user = userService.getById(id);
        if (user==null){
            User registerUser=new User();
            registerUser.setId(id);
            registerUser.setName(name);
            registerUser.setRole(Constant.ROLE_CUSTOMER);
            registerUser.setPassword(password);
            userService.save(registerUser);
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultEnum.ERROR.getCode(),"该账号已被注册");
    }

    @ApiOperation("商家注册")
    @PostMapping("/register/seller")
    public Result registerSeller(String id,String name,String password){
        final User user = userService.getById(id);
        if (user==null){
            User registerUser=new User();
            registerUser.setId(id);
            registerUser.setName(name);
            registerUser.setRole(Constant.ROLE_SELLER);
            registerUser.setPassword(password);
            userService.save(registerUser);
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultEnum.ERROR.getCode(),"该账号已被注册");
    }

    @Autowired
    private FilePathService filePathService;

    @ApiOperation("用户上传头像")
    @PostMapping("/upload")
    public Result upload(HttpServletRequest request,@RequestParam("file") MultipartFile file) {
        String path=filePathService.Upload(file);
        if(path=="fail")
            return ResultUtil.error(500,"上传失败");
        else {
            String id=(String)request.getSession().getAttribute("id");
            User user=userService.getById(id);
            if(user!=null){
                user.setPicture(path);
                userService.updateById(user);
                return ResultUtil.success();
            }
            else
                return ResultUtil.error(500,"未找到指定用户");
        }
    }

    @Value("${domainName}")
    private String domainName;
    @ApiOperation("获取用户头像")
    @PostMapping("/getPicture")
    public Result getPicture(HttpServletRequest request){
        String id=(String)request.getSession().getAttribute("id");
        User user=userService.getById(id);
        if(user!=null){
            String picture=user.getPicture();
            return ResultUtil.success(domainName+picture);
        }
        else
            return ResultUtil.error(500,"未找到指定用户");
    }

    @ApiOperation("修改密码")
    @PostMapping("/modifyPwd")
    public Result modifyPwd(HttpServletRequest request,String pwd){
        String id=(String)request.getSession().getAttribute("id");
        User user=userService.getById(id);
        if(user==null)
            return ResultUtil.error(500,"该用户不存在");
        user.setPassword(pwd);
        userService.updateById(user);
        return ResultUtil.success();
    }
    @ApiOperation("修改用户昵称")
    @PostMapping("/modifyName")
    public Result modifyName(HttpServletRequest request,String name){
        String id=(String)request.getSession().getAttribute("id");
        User user=userService.getById(id);
        if(user==null)
            return ResultUtil.error(500,"该用户不存在");
        user.setName(name);
        userService.updateById(user);
        return ResultUtil.success();
    }

    @ApiOperation("退出登录")
    @PostMapping("/quite")
    public Result quite(HttpServletRequest request){
        request.getSession().invalidate();
        return ResultUtil.success();
    }
}
